package org.blueoxygen.kukuruyuk.actions.plasma;
import java.util.ArrayList;
import java.util.List;
import org.blueoxygen.cimande.gx.entity.GxDroplistValue;
import org.blueoxygen.cimande.persistence.PersistenceAware;
import org.blueoxygen.cimande.persistence.PersistenceManager;
import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactory;
import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactoryAware;
import org.blueoxygen.cimande.security.SessionCredentials;
import org.blueoxygen.cimande.security.SessionCredentialsAware;
import org.blueoxygen.kukuruyuk.entity.Plasma;
import org.blueoxygen.kukuruyuk.entity.Location;
import com.opensymphony.xwork2.ActionSupport;

public class PlasmaForm extends ActionSupport
implements PersistenceAware, HibernateSessionFactoryAware, SessionCredentialsAware{
	protected PersistenceManager manager;
	protected HibernateSessionFactory hsf;
	protected SessionCredentials sessionCredentials;
	private Location location = new Location ();
	private GxDroplistValue droplistValue = new GxDroplistValue();
	private List<GxDroplistValue> droplistValues = new ArrayList<GxDroplistValue>();
	
	private Plasma plasma = new Plasma();
	private List<Plasma> plasmas = new ArrayList<Plasma>();
	
	private String codeDefault = "";
	private int intCode;
	
	public String execute(){
		if(getPlasma().getId() != null &&
				!"".equalsIgnoreCase(getPlasma().getId().trim())){
				setPlasma((Plasma) manager.getById(Plasma.class, getPlasma().getId()));
		}
		setDroplistValues(manager.getList("SELECT w FROM "+GxDroplistValue.class.getName()+" w WHERE w.name.id='8a8a8a931f5df886011f5e0a8cb80001'",null, null));
		setPlasmas(manager.getList("SELECT m FROM " + Plasma.class.getName() + " m WHERE m.logInformation.activeFlag='1'", null, null));
		intCode = getPlasmas().size();
		if(intCode == 0){
			setCodeDefault("PL00001");
		}else if (intCode < 10) {
			setCodeDefault("PL0000"+(intCode+1));
		}else if(intCode < 100){
			setCodeDefault("PL000"+(intCode+1));
		}else if(intCode < 1000){
			setCodeDefault("PL00"+(intCode+1));
		}else if(intCode < 10000){
			setCodeDefault("PL0"+(intCode+1));
		}else if(intCode < 100000){
			setCodeDefault("PL"+(intCode+1));
		}	
		
			return SUCCESS;
		}

	public GxDroplistValue getDroplistValue() {
		return droplistValue;
	}

	public void setDroplistValue(GxDroplistValue droplistValue) {
		this.droplistValue = droplistValue;
	}

	public List<GxDroplistValue> getDroplistValues() {
		return droplistValues;
	}

	public void setDroplistValues(List<GxDroplistValue> droplistValues) {
		this.droplistValues = droplistValues;
	}

	public List<Plasma> getPlasmas() {
		return plasmas;
	}

	public PersistenceManager getManager() {
		return manager;
	}

	public void setManager(PersistenceManager manager) {
		this.manager = manager;
	}

	public HibernateSessionFactory getHsf() {
		return hsf;
	}

	public void setHsf(HibernateSessionFactory hsf) {
		this.hsf = hsf;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public SessionCredentials getSessionCredentials() {
		return sessionCredentials;
	}

	public void setPlasmas(List<Plasma> plasmas) {
		this.plasmas = plasmas;
	}

	@Override
	public void setPersistenceManager(PersistenceManager persistenceManager) {
		// TODO Auto-generated method stub
		this.manager = persistenceManager;
	}

	@Override
	public void setHibernateSessionFactory(HibernateSessionFactory arg0) {
		// TODO Auto-generated method stub
		this.hsf = arg0;
	}

	@Override
	public void setSessionCredentials(SessionCredentials sessionCredentials) {
		// TODO Auto-generated method stub
		this.sessionCredentials = sessionCredentials;
	}

	public String getCodeDefault() {
		return codeDefault;
	}

	public void setCodeDefault(String codeDefault) {
		this.codeDefault = codeDefault;
	}

	public int getIntCode() {
		return intCode;
	}

	public void setIntCode(int intCode) {
		this.intCode = intCode;
	}

	public Plasma getPlasma() {
		return plasma;
	}

	public void setPlasma(Plasma plasma) {
		this.plasma = plasma;
	}
	
}
	
